//
//  XMBrowsHistoryView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/28.
//

#import "XMBrowsHistoryView.h"
#import "XMMyOrderTopTabView.h"
#import "XMBrowsHistoryCell.h"
#import "XMBrowseVideoCell.h"
#import "XMBrowsHistoryReusableView.h"

@interface XMBrowsHistoryView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) XMMyOrderTopTabView *topTabView;

@property (nonatomic, strong) NSMutableArray *browsHistoryModelArr;

@property (nonatomic, assign) NSInteger tapSelectIndex;

@end

@implementation XMBrowsHistoryView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"浏览记录";
    self.navView.backgroundColor = kColorHEX(0xffffff);
    self.backgroundColor = kColorHEX(0xF0F0F0);
    _tapSelectIndex = 0;
    [self addSubview:self.topTabView];
    [self addSubview:self.collectionView];
    
    UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(12.0) frame:CGRectMake(self.width - KScaleWidth(10.0) - KScaleWidth(44.0), self.navTitleLabel.y, KScaleWidth(44.0), self.navTitleLabel.height) title:@"清空" titleColor:kColorHEX(0x000000)];
    [clearBtn addTarget:self action:@selector(clearAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.navView addSubview:clearBtn ];
    
    XMWeakSelf;
    _topTabView.itemSelectBlock = ^(NSInteger itemSelect) {
        if (weakSelf.tapSelectIndex == itemSelect) {
            return;
        }
        weakSelf.tapSelectIndex = itemSelect;
        NSLog(@"tap选择");
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(tapSelect:)]) {
            [weakSelf.delegate tapSelect:itemSelect];
        }
    };
}

- (void)setContentWithHistoryVideoModelArr:(NSArray *)historyModelArr
                                      page:(NSInteger)page {
    [_collectionView.mj_header endRefreshing];
    if (page == 1) {
        [self.browsHistoryModelArr removeAllObjects];
    }
    if (historyModelArr.count < 10) {
        [_collectionView.mj_footer endRefreshingWithNoMoreData];
    }
    else {
        [_collectionView.mj_footer endRefreshing];
    }
    [self.browsHistoryModelArr addObjectsFromArray:historyModelArr];
    [_collectionView reloadData];
}

//清空浏览记录
- (void)clearAction:(UIButton *)button {
    if (_tapSelectIndex == 0) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(clearBrowseVideo)]) {
            [self.delegate clearBrowseMeet];
        }
    }
    else {
        if (self.delegate && [self.delegate respondsToSelector:@selector(clearBrowseMeet)]) {
            [self.delegate clearBrowseVideo];
        }
    }
}

#pragma mark - Delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section {
    return self.browsHistoryModelArr.count;
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    return CGSizeMake(self.width, KScaleWidth(46.0));
//}
//
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//        XMBrowsHistoryReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"XMBrowsHistoryReusableViewName" forIndexPath:indexPath];
//        [headView setContentWithDateStr:@"2025-05-28"];
//        return headView;
//    }
//    return nil;
//}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_tapSelectIndex == 0) {
        CGFloat itemWidth = floor((self.width - KScaleWidth(8.0)*3)/2);
        return CGSizeMake(itemWidth, KScaleWidth(305.0));
    }
    else {
        CGFloat itemWidth = floor((self.width - KScaleWidth(8.0)*3)/2);
        return CGSizeMake(itemWidth, KScaleWidth(285.0));
    }
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_tapSelectIndex == 0) {
        XMBrowsHistoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XMBrowsHistoryCellName" forIndexPath:indexPath];
        [cell setContentWithGoodDic:_browsHistoryModelArr[indexPath.row]];
        return cell;
    }
    else {
        XMBrowseVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XMBrowseVideoCellName" forIndexPath:indexPath];
        [cell setContentWithBrowseProductModel:_browsHistoryModelArr[indexPath.row]];
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (_tapSelectIndex == 0) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(meetCheckDetailWithServiceId:)]) {
            XMMyBrowseProductModel *productModel = _browsHistoryModelArr[indexPath.row];
            [self.delegate meetCheckDetailWithServiceId:productModel.serviceId];
        }
    }
}

#pragma mark - Init
- (XMMyOrderTopTabView *)topTabView {
    if (!_topTabView) {
        CGFloat space = (self.width - KScaleWidth(55.0)*2)/2;
        _topTabView = [[XMMyOrderTopTabView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, KScaleWidth(43.0)) titleArr:@[@"产品", @"作品"] space:space lineColor:kColorHEX(0xFFD800) lineWidth:KScaleWidth(25.0) fontSize:KScaleWidth(15.0) titleColor:kColorHEX(0xBDBDBD) selectColor:kColorHEX(0x333333)];
    }
    return _topTabView;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat itemWidth = floor((self.width - KScaleWidth(8.0)*3)/2);
        layout.itemSize = CGSizeMake(itemWidth, KScaleWidth(305.0));
        layout.minimumLineSpacing = KScaleWidth(8.0);
        layout.minimumInteritemSpacing = KScaleWidth(8.0);
        layout.sectionInset = UIEdgeInsetsMake(0, KScaleWidth(8.0), 0, KScaleWidth(8.0));
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.topTabView.maxY + KScaleWidth(8.0), self.width, self.height - _topTabView.maxY - KScaleWidth(8.0)) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = kColorHEX(0xF7F7F7);
        [_collectionView registerClass:[XMBrowsHistoryCell class] forCellWithReuseIdentifier:@"XMBrowsHistoryCellName"];
        [_collectionView registerClass:[XMBrowsHistoryReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"XMBrowsHistoryReusableViewName"];
        [_collectionView registerClass:[XMBrowseVideoCell class] forCellWithReuseIdentifier:@"XMBrowseVideoCellName"];
        XMWeakSelf;
        _collectionView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(refreshData)]) {
                [weakSelf.delegate refreshData];
            }
        }];
        
        _collectionView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(loadMore)]) {
                [weakSelf.delegate loadMore];
            }
        }];
    }
    return _collectionView;
}

- (NSMutableArray *)browsHistoryModelArr {
    if (!_browsHistoryModelArr) {
        _browsHistoryModelArr = [NSMutableArray array];
    }
    return _browsHistoryModelArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
